package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.query.RegionQuery;
import cn.wolfcode.luowowo.article.service.IRegionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class RegionServiceImpl implements IRegionService {
    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private DestinationMapper destinationMapper;

    @Override
    public PageInfo<Region> listAll(RegionQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Region> listData = regionMapper.selectAll(qo);
        return new PageInfo<>(listData);
    }

    @Override
    public void changeHotValue(int hot, Long id) {
        regionMapper.updateIsHotByPrimaryId(hot,id);
    }

    @Override
    public List<Destination> getDestByRegionId(Long rid) {
        Region region = regionMapper.selectByPrimaryKey(rid);
        return destinationMapper.selectByIds(region.getRefIds());
    }

    @Override
    public void delete(Long id) {
        regionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Region region) {
        if(region.getId() == null){
            regionMapper.insert(region);
        }else{
            regionMapper.updateByPrimaryKey(region);
        }
    }

    @Override
    public List<Region> selectForList(RegionQuery qo) {
        return regionMapper.selectAll(qo);
    }

    @Override
    public List<Destination> getHotDestByRegionId(Long regionId) {
        DestinationQuery qo = new DestinationQuery();
        if (regionId == -1) {
            qo.setParentId(1L);
            qo.setIshot(1);
            return destinationMapper.selectForList(qo);
        }
        return getDestByRegionId(regionId);
    }
}
